package com.geniusk.system.controller;

import com.geniusk.common.core.JSONResult;
import com.geniusk.file.utils.MinioUtil;
import com.geniusk.security.utils.SecurityUtil;
import com.geniusk.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 个人信息接口
 */
@RestController
@RequestMapping("/system/profile")
public class ProfileController {

    @Autowired
    private MinioUtil minioUtil;

    @Autowired
    private UserService userService;

    /**
     * 头像上传
     */
    @PostMapping("/uploadAvatar")
    public JSONResult uploadAvatar(@RequestParam("file") MultipartFile file) {
        try {
            // 上传并返回访问地址
            String avatarUrl = minioUtil.uploadFile(file);
            Map<String,Object> result = new HashMap<>();
            result.put("avatarUrl",avatarUrl);
            userService.updateAvatar(SecurityUtil.getUserId(),avatarUrl);
            return JSONResult.ok(result);
        } catch (Exception e) {
            return JSONResult.errorMsg(e.getMessage());
        }
    }
}
